package org.example.basic.functionclass;


import org.example.basic.entity.UserEntity;

import java.time.LocalDate;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;


/**
 * 函数式操作
 * @author songyafei
 */
public class BiPredicateFunction {


    public static void main(String[] args) {
        //BiPredicate<T,U>
        //代表了一个两个参数的boolean值方法
        BiPredicate<String,String> stringBiPredicate = (s1, s2)-> s1.toLowerCase(Locale.ROOT).equals(s2.toLowerCase(Locale.ROOT));
        boolean test = stringBiPredicate.test("AVC", "avc");
        System.out.println("string 测试"+test);

        List<UserEntity> userList = new ArrayList<>();
        List<UserEntity> userList1 = new ArrayList<>();

        UserEntity u1 = new UserEntity();
        u1.setAge(19);
        u1.setUserId(1L);
        u1.setCreatedDate(LocalDate.of(2010,01,20));
        u1.setName("张明");

        UserEntity u2 = new UserEntity();
        u2.setAge(22);
        u2.setUserId(2L);
        u2.setCreatedDate(LocalDate.of(2011,01,20));
        u2.setName("刘乐");

        userList.add(u1);
        userList.add(u2);
        userList1.add(u2);

        BiPredicate< List<UserEntity>, List<UserEntity>> userBiPredicate = (l1,l2)->{
            if (l1 == null||l2==null) {
                return false;
            }
            Map<Long, Map<Long, Integer>> userIdMap = l2.stream().collect(Collectors.groupingBy(UserEntity::getUserId, Collectors.toMap(UserEntity::getUserId, UserEntity::getAge)));
            List<UserEntity> userEntityStream = l1.stream().filter(m -> null != userIdMap.get(m.getUserId())).collect(Collectors.toList());
            return userEntityStream.size()>0;
        };
        boolean test1 = userBiPredicate.test(userList, userList1);
        System.out.println("list 测试"+test1);

        //-----------------------------------------------------
        List<String> listString1 = new ArrayList<>(Arrays.asList("gf","lk","mm","A"));
        List<String> listString2 = new ArrayList<>(Arrays.asList("gffff","lsk","msm","A"));
        boolean checkString = checkString(listString1,
                listString2,
                (l1A, l1B) -> l1A.contains("A") && l1B.contains("A"),
                (l2A, l2B) -> l2A.size() > 0 && l2B.size() > 0);
        System.out.println("string 类型多参数校验结果"+checkString);

        boolean check = check(userList, userList1,
                (u1A, u1B) -> u1A.size() > 0 && u1B.size() > 0,
                (u2A, u2B) -> u2A.size() < 0 || u2B.size() < 0);
        System.out.println("check List<UserEntity>"+check);
    }


    private static boolean check(List<UserEntity> list1,
                                 List<UserEntity> list2,
                                 BiPredicate< List<UserEntity>, List<UserEntity>> biPredicate1,
                                 BiPredicate< List<UserEntity>, List<UserEntity>> biPredicate2){
        return biPredicate1.or(biPredicate2).test(list1,list2);
    }

    private static boolean checkString(List<String> list1,
                                 List<String> list2,
                                 BiPredicate< List<String>, List<String>> biPredicate1,
                                 BiPredicate< List<String>, List<String>> biPredicate2){
        return biPredicate1.and(biPredicate2).test(list1,list2);
    }
}
